package ldh.msg.ui.helper;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.SwingConstants;

public class MsgUIUtils {


	public synchronized static JButton makeBtn(String name, int width, int height) {
		JButton btn = new JButton(name);
		btn.setActionCommand(name);
		btn.setMinimumSize(new Dimension(width, height));
		btn.setMaximumSize(new Dimension(width, height));
		btn.setPreferredSize(new Dimension(width, height));
		btn.setFont(new Font("Dialog", 5, 9));
		btn.setBackground(Color.WHITE);
		btn.setMargin(new Insets(1, 1, 1, 1));
		btn.setVerticalAlignment(SwingConstants.CENTER);
		btn.setHorizontalAlignment(SwingConstants.CENTER);
		return btn;
	}
	
	public synchronized static JButton makeImgBtn(String name, String filePath, int width, int height, int txtWidth, int txtHeight) {
		ImageIcon reloadIcon = null;
		JButton btn = null;
		btn = new JButton(name);
		reloadIcon = resizeImg(filePath, width, height);
		if (reloadIcon != null) {
			btn = new JButton(reloadIcon);
			btn.setActionCommand(name);
			btn.setToolTipText(name);
			btn.setMinimumSize(new Dimension(width, height));
			btn.setMaximumSize(new Dimension(width, height));
			btn.setPreferredSize(new Dimension(width, height));
		} else {
			btn = makeBtn(name, txtWidth, txtHeight);
		}
		return btn;
	}
	
	public synchronized static ImageIcon resizeImg(String filePath, int width, int height) {
		BufferedImage image = null;
		ImageIcon reloadIcon = null;
		try {
			image = ImageIO.read(new File(filePath));
			reloadIcon = new ImageIcon( image.getScaledInstance(width-1, height-1, Image.SCALE_SMOOTH) );
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		return reloadIcon;
	}	
}
